//
//  Liu70MJRefreshCollectionView.m
//
//  Created by Liu70 on 2018/1/19.
//  Copyright © 2018年 Liu70. All rights reserved.
//

#import "Liu70MJRefreshCollectionView.h"

@implementation Liu70MJRefreshCollectionView

#pragma mark - set
- (void)setRefreshFirstPage:(NSInteger)refreshFirstPage {
    _refreshFirstPage = refreshFirstPage;
    self.refreshPage = refreshFirstPage - 1;
}

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    self = [super initWithFrame:frame collectionViewLayout:layout];
    [self initRefreshData];
    [self setMJHeaderFooter];
    return self;
}

- (void)initRefreshData {
    self.refreshFirstPage = 1;
    self.refreshDataArray = [NSMutableArray array];
    self.refreshPageCount = 5;
}

- (void)setMJHeaderFooter {
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handleMJHeaderLogic];
    }];
    self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self handleMJFooterLogic];
    }];
    self.mj_footer.hidden = YES;
}

- (void)handleMJHeaderLogic {
    self.refreshPage = self.refreshFirstPage;
    self.refreshDataArray = [NSMutableArray array];
    [self beginRefresh];
}

- (void)handleMJFooterLogic {
    self.refreshPage += 1;
    [self beginRefresh];
}

#pragma mark - more methods
- (void)beginRefresh {
    [self.refreshDelegate liu70MJRefreshCollectionView:self didBeginRefresh:[self refreshData]];
}

- (void)endRefresh {
    [self.mj_header endRefreshing];
    [self.mj_footer endRefreshing];
}

- (BOOL)isNoMoreData:(NSMutableArray*)newDataArray {
    return ((self.refreshDataArray.count % self.refreshPageCount != 0) || (newDataArray.count <= 0));
}

#pragma mark - 核心bolck
- (Liu70MJRefreshCollectionView * (^)(NSMutableArray *))refreshData {
    __weak typeof(self) weakSelf = self;
    Liu70MJRefreshCollectionView * (^block)(NSMutableArray *) = ^(NSMutableArray *newDataArray) {
        [weakSelf endRefresh];
        weakSelf.refreshDataArray = (NSMutableArray*)[weakSelf.refreshDataArray arrayByAddingObjectsFromArray:newDataArray];
        [weakSelf reloadData];
        weakSelf.mj_footer.hidden = [weakSelf isNoMoreData:newDataArray];
        return weakSelf;
    };
    return block;
}

#pragma mark - 链式编程
- (Liu70MJRefreshCollectionView * (^)(id))refreshDelegateFast {
    Liu70MJRefreshCollectionView * (^block)(id) = ^(id object) {
        self.refreshDelegate = object;
        return self;
    };
    return block;
}

- (Liu70MJRefreshCollectionView * (^)(NSInteger))refreshFirstPageFast {
    Liu70MJRefreshCollectionView * (^block)(NSInteger) = ^(NSInteger firstPage) {
        self.refreshFirstPage = firstPage;
        return self;
    };
    return block;
}

- (Liu70MJRefreshCollectionView * (^)(NSInteger))refreshPageCountFast {
    Liu70MJRefreshCollectionView * (^block)(NSInteger) = ^(NSInteger pageCount) {
        self.refreshPageCount = pageCount;
        return self;
    };
    return block;
}

@end












